<?php

/**
 * profile filter form base class.
 *
 * @package    sfwixet
 * @subpackage filter
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormFilterGeneratedTemplate.php 29570 2010-05-21 14:49:47Z Kris.Wallsmith $
 */
abstract class BaseprofileFormFilter extends BaseFormFilterDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'visits'          => new sfWidgetFormFilterInput(),
      'gender'          => new sfWidgetFormFilterInput(),
      'uid'             => new sfWidgetFormFilterInput(),
      'birthday'        => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate())),
      'city_id'         => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('city'), 'add_empty' => true)),
      'country_id'      => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('country'), 'add_empty' => true)),
      'town_id'         => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('town'), 'add_empty' => true)),
      'created_at'      => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => false)),
      'updated_at'      => new sfWidgetFormFilterDate(array('from_date' => new sfWidgetFormDate(), 'to_date' => new sfWidgetFormDate(), 'with_empty' => false)),
      'places_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'place')),
      'workplaces_list' => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'workplace')),
      'schools_list'    => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'school')),
      'cityplace_list'  => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'city')),
      'groups_list'     => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'aclGroup')),
    ));

    $this->setValidators(array(
      'visits'          => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'gender'          => new sfValidatorSchemaFilter('text', new sfValidatorInteger(array('required' => false))),
      'uid'             => new sfValidatorPass(array('required' => false)),
      'birthday'        => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDate(array('required' => false)), 'to_date' => new sfValidatorDateTime(array('required' => false)))),
      'city_id'         => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('city'), 'column' => 'city_id')),
      'country_id'      => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('country'), 'column' => 'country_id')),
      'town_id'         => new sfValidatorDoctrineChoice(array('required' => false, 'model' => $this->getRelatedModelName('town'), 'column' => 'city_id')),
      'created_at'      => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59')))),
      'updated_at'      => new sfValidatorDateRange(array('required' => false, 'from_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 00:00:00')), 'to_date' => new sfValidatorDateTime(array('required' => false, 'datetime_output' => 'Y-m-d 23:59:59')))),
      'places_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'place', 'required' => false)),
      'workplaces_list' => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'workplace', 'required' => false)),
      'schools_list'    => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'school', 'required' => false)),
      'cityplace_list'  => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'city', 'required' => false)),
      'groups_list'     => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'aclGroup', 'required' => false)),
    ));

    $this->widgetSchema->setNameFormat('profile_filters[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function addPlacesListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.placeUser placeUser')
      ->andWhereIn('placeUser.user_id', $values)
    ;
  }

  public function addWorkplacesListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.workplaceUser workplaceUser')
      ->andWhereIn('workplaceUser.user_id', $values)
    ;
  }

  public function addSchoolsListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.schoolUser schoolUser')
      ->andWhereIn('schoolUser.user_id', $values)
    ;
  }

  public function addCityplaceListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.cityUser cityUser')
      ->andWhereIn('cityUser.user_id', $values)
    ;
  }

  public function addGroupsListColumnQuery(Doctrine_Query $query, $field, $values)
  {
    if (!is_array($values))
    {
      $values = array($values);
    }

    if (!count($values))
    {
      return;
    }

    $query
      ->leftJoin($query->getRootAlias().'.aclProfileGroup aclProfileGroup')
      ->andWhereIn('aclProfileGroup.group_id', $values)
    ;
  }

  public function getModelName()
  {
    return 'profile';
  }

  public function getFields()
  {
    return array(
      'user_id'         => 'Number',
      'visits'          => 'Number',
      'gender'          => 'Number',
      'uid'             => 'Text',
      'birthday'        => 'Date',
      'city_id'         => 'ForeignKey',
      'country_id'      => 'ForeignKey',
      'town_id'         => 'ForeignKey',
      'created_at'      => 'Date',
      'updated_at'      => 'Date',
      'places_list'     => 'ManyKey',
      'workplaces_list' => 'ManyKey',
      'schools_list'    => 'ManyKey',
      'cityplace_list'  => 'ManyKey',
      'groups_list'     => 'ManyKey',
    );
  }
}
